package com.sl.au.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.ContractInfo;
import com.sl.au.entity.ScadaBasePoint;
import com.sl.au.entity.ScadaRtuBaseInfo;
import com.sl.au.entity.User;
import com.sl.au.entity.scadaModelPointRelation;
import com.sl.au.repository.ExdeScadaRtuBaseRepository;
import com.sl.au.repository.QuoteDeviceRepository;
import com.sl.au.repository.ScadaBasePointRepository;
import com.sl.au.repository.scadaModelPointRelatioRepository;
import com.sl.au.service.ExdeScadaRtuBaseService;
import com.sl.au.service.QueryService;

@Service("ExdeScadaRtuBaseService")
@Transactional
public class ExdeScadaRtuBaseServiceImpl extends BaseServiceImpl implements ExdeScadaRtuBaseService {

	@Autowired
	ExdeScadaRtuBaseRepository exdeScadaRtuBaseRepository;
	
	@Autowired
	ScadaBasePointRepository scadaBasePointRepository;
	
	@Autowired
	scadaModelPointRelatioRepository scadamodelPointRelatioRepository;
	
	@Autowired
	QueryService queryService;
	
	@Override
	public ExdeScadaRtuBaseRepository getExdeScadaRtuBaseRepository() {
		return this.exdeScadaRtuBaseRepository;
	}
	
	@Override
	public ScadaBasePointRepository getScadaBasePointRepository() {
		return this.scadaBasePointRepository;
	}
	
	@Override
	public List<ScadaRtuBaseInfo> getScadaRtuBaseInfos(int start, int length, Map<String, String> condition, User user, String orderName, String dir) {
		Page<ScadaRtuBaseInfo> pages = null;
		String dataFilter = condition.get("dataFilter");
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(dataFilter.equals("所有")){
			pages = exdeScadaRtuBaseRepository.getScadaRtuBaseInfos(condition.get("key"), pageable);
		}else if(dataFilter.equals("本部门")){
			pages = exdeScadaRtuBaseRepository.getScadaRtuBaseInfos(condition.get("key"), condition.get("superior"), pageable);
		}else if(dataFilter.equals("本片区")){
			pages = exdeScadaRtuBaseRepository.getScadaRtuBaseInfos(condition.get("key"), condition.get("superior"), condition.get("areaName"),  pageable);
		}else if(dataFilter.equals("本人")){
			pages = exdeScadaRtuBaseRepository.getScadaRtuBaseInfosByUser(condition.get("key"), condition.get("userId"), pageable);
		}
		return pages.getContent();
	}

	@Override
	public long getScadaRtuBaseInfosCount(int i, int maxValue, Map<String, String> condition, User user,
			String orderName, String dir) {
		String dataFilter = condition.get("dataFilter");
		if(dataFilter.equals("所有")){
			return this.exdeScadaRtuBaseRepository.getScadaRtuBaseInfosCount(condition.get("key"));
		}else if(dataFilter.equals("本部门")){
			return this.exdeScadaRtuBaseRepository.getScadaRtuBaseInfosCount(condition.get("key"), condition.get("superior"));
		}else if(dataFilter.equals("本片区")){
			return this.exdeScadaRtuBaseRepository.getScadaRtuBaseInfosCount(condition.get("key"), condition.get("superior"), condition.get("areaName"));
		}else{
			return this.exdeScadaRtuBaseRepository.getScadaRtuBaseInfosByUserCount(condition.get("key"), condition.get("userId"));
		}
	}

	@Override
	public List<ScadaBasePoint> getScadaBasePoints(int start, int length, Map<String, String> condition,
			String orderName, String dir) {
		Page<ScadaBasePoint> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = scadaBasePointRepository.getScadaBasePointsbyScadaRtuBaseInfoId(condition.get("ScadaRtuBaseInfoId"), condition.get("key"), pageable);
		return pages.getContent();
	}

	@Override
	public long getScadaBasePointsCount(int i, int maxValue, Map<String, String> condition, String orderName,
			String dir) {
		return this.scadaBasePointRepository.getScadaBasePointsbyScadaRtuBaseInfoIdCount(condition.get("ScadaRtuBaseInfoId"), condition.get("key"));
	}

	/**
	 * 删除点位
	 */
	@Override
	public void deleteScadaBasePoint(String ids) {
		StringBuilder hql = buildCombinedQueryHql("ScadaBasePoint", "id", ids, "createTime", "desc");
		Query query=queryService.createQueryHql(hql.toString());
		System.out.println(hql.toString());
		@SuppressWarnings("unchecked")
		List<ScadaBasePoint> ScadaBasePoints = query.getResultList();//查找点位
		
		StringBuilder hql1 = buildCombinedQueryHql("scadaModelPointRelation", "pointId", ids, "createTime", "desc");
		Query query1=queryService.createQueryHql(hql1.toString());
		System.out.println(hql1.toString());
		@SuppressWarnings("unchecked")
		List<scadaModelPointRelation> lists = query1.getResultList();//点位对应模型的关系
		
		scadamodelPointRelatioRepository.delete(lists);
		scadaBasePointRepository.delete(ScadaBasePoints);
	}

	/**
	 * 删除单元
	 */
	@Override
	public void deleteScadaRtuBaseInfo(String ids) {
		StringBuilder hql = buildCombinedQueryHql("ScadaRtuBaseInfo", "id", ids, "createTime", "desc");
		Query query=queryService.createQueryHql(hql.toString());
		System.out.println(hql.toString());
		@SuppressWarnings("unchecked")
		List<ScadaRtuBaseInfo> ScadaRtuBaseInfos = query.getResultList();//查找单元
		
		StringBuilder hql1 = buildCombinedQueryHql("ScadaBasePoint", "scadaRtuBaseInfoId", ids, "createTime", "desc");
		Query query1=queryService.createQueryHql(hql1.toString());
		System.out.println(hql1.toString());
		@SuppressWarnings("unchecked")
		List<ScadaBasePoint> ScadaBasePoints = query1.getResultList();//查找单位对应的点位
		
		String pointIds = "";
		for(ScadaBasePoint scadaBasePoint : ScadaBasePoints){
			pointIds += "'" + scadaBasePoint.getId() + "',";
		}
		pointIds = pointIds.substring(0, pointIds.length()-1);
		StringBuilder hql2 = buildCombinedQueryHql("scadaModelPointRelation", "pointId", pointIds, "createTime", "desc");
		Query query2=queryService.createQueryHql(hql2.toString());
		System.out.println(hql2.toString());
		@SuppressWarnings("unchecked")
		List<scadaModelPointRelation> lists = query2.getResultList();//点位对应模型的关系
		
		scadamodelPointRelatioRepository.delete(lists);
		scadaBasePointRepository.delete(ScadaBasePoints);
		exdeScadaRtuBaseRepository.delete(ScadaRtuBaseInfos);
		
	}

	public StringBuilder buildCombinedQueryHql(String entityName, String selectedProperty, String ids, String orderName, String dir){
		StringBuilder hql = new StringBuilder();
		hql.append("select e from " + entityName + " e where e.id is not null ");
		hql.append(" and e." + selectedProperty + " in (" + ids + ")");
		hql.append(" order by " + orderName + " " + dir);
		return hql;
	}

}
